package com.hdax.dm.scheduler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.entity.scheduler.Scheduler;
import com.hdax.dm.entity.scheduler.SchedulerSeat;
import com.hdax.dm.entity.scheduler.SchedulerSeatPrice;
import com.hdax.dm.scheduler.dto.ChooseSeatScheduleDto;
import com.hdax.dm.scheduler.dto.SeatDto;
import com.hdax.dm.scheduler.mappers.SchedulerSeatMapper;
import com.hdax.dm.scheduler.mapstruct.SeatInfoDtoMapper;
import com.hdax.dm.scheduler.mapstruct.SeatPriceDtoMapper;
import com.hdax.dm.scheduler.service.SchedulerSeatPriceService;
import com.hdax.dm.scheduler.service.SchedulerSeatService;
import com.hdax.dm.scheduler.service.SchedulerService;
import com.hdax.dm.utils.CommonResponse;
import com.hdax.dm.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SchedulerSeatServiceImpl extends ServiceImpl<SchedulerSeatMapper, SchedulerSeat> implements SchedulerSeatService {

    @Autowired
    private SchedulerService schedulerService;
    @Autowired
    private SchedulerSeatPriceService priceService;
    @Autowired
    private SeatInfoDtoMapper dtoMapper;
    @Autowired
    private SeatPriceDtoMapper priceMapper;
    @Autowired
    private SchedulerSeatService seatService;


    @Override
    public CommonResponse<SeatDto> getSeats(Long scheduleId) {
        //java8 stream groupby
        Map<Long,List<SchedulerSeat>> map = this.seats(scheduleId,false).stream().collect(Collectors.groupingBy(SchedulerSeat::getX));

        List<String> seatArray = new ArrayList<>();
        map.forEach((key,value)->{
            StringBuffer buffer = new StringBuffer();
            value.forEach(seat->{
                if (seat.getStatus() == 0) {
                    buffer.append("_");
                }else{
                    if (seat.getAreaLevel() == 1L) {
                        buffer.append("a");
                    } else if (seat.getAreaLevel() == 2L) {
                        buffer.append("b");
                    } else if (seat.getAreaLevel() == 3L) {
                        buffer.append("c");
                    }
                }
            });
            seatArray.add(buffer.toString());
        });
        SeatDto seatDto = new SeatDto();
        seatDto.setSeatArray(seatArray);

        return ResponseUtil.returnSuccess(seatDto);
    }

    @Override
    public CommonResponse<ChooseSeatScheduleDto> seatAvaliable(Long scheduleId) {
        //查询排期的详情
        Scheduler scheduler = schedulerService.scheduler(scheduleId);
        ChooseSeatScheduleDto dto = new ChooseSeatScheduleDto();
        dto.setScheduleId(scheduler.getId());
        dto.setCinemaId(scheduler.getCinemaId());
        List<SchedulerSeat> seats = seats(scheduleId,true);
        dto.setSeatInfoList(dtoMapper.toTargetList(seats));//查询所有可以被购买的座位
        List<SchedulerSeatPrice> prices = priceService.prices(scheduleId);
        dto.setSeatPriceList(priceMapper.toTargetList(prices));
        return ResponseUtil.returnSuccess(dto);
    }

    @Override
    public List<SchedulerSeat> seats(Long scheduleId, boolean isNotAll) {
        List<SchedulerSeat> list = baseMapper.seats(scheduleId,isNotAll);
        return list;
    }

    @Override
    public int updateSeat(SchedulerSeat seat) {
        return baseMapper.updateSeat(seat);
    }

    @Override
    public boolean seatRollback(String orderNo) {
        return baseMapper.updateSeat2(orderNo)>0;
    }

    @Override
    public int soldSeat(String orderNo) {
        return baseMapper.sold(orderNo);
    }

    @Override
    public int updateSeatOne(String orderNo) {
        return baseMapper.updateSeatOne(orderNo);
    }

    @Override
    public int updateSeatTwo(String orderNo) {
        return baseMapper.updateSeatTwo(orderNo);
    }

}
